﻿

using HBase;

using hconnect;
using System;
using System.Collections.Generic;

namespace hCommand {
    public abstract class SuperiorAllocateJobTask:JobAbleTask {

        public abstract List<BaseTask> analyseTaskModel(AbsStatus gcs);
        public virtual Type getSynchro() {
            throw new NotImplementedException();
        }
        public override bool excute() {
            var type = getSynchro();
            AbsStatus gcs = StatusCache.getStatus(type);
            if (gcs == null) {
                consoleOnly(InfoCode.consleOnly_Status_null,"状态获取异常");
                return false;
            }
            perCheck();
            if (!gcs.isIdle()) {

                return true;
            }
            if (!chechModeDB()) {
                return true;
            }
            List<BaseTask> robotTask = analyseTaskModel(gcs);
            if (robotTask != null && robotTask.Count > 0) {
                DoOutAndShiftTask(robotTask);
            } else {
                robotTask = preExecution();
                if (robotTask != null && robotTask.Count > 0) {
                    DoOutAndShiftTask(robotTask);
                } else {
                    //consoleOnly(InfoCode.consleOnly_job_idle, "isIdle");
                }
            }
            return true;
        }

        public virtual bool chechModeDB() {
            return true;
        }
        public virtual void perCheck() {

        }
        public virtual List<BaseTask> preExecution() {
            return new List<BaseTask>();
        }

        public List<T> getCanWorkNewTask<T>() where T : BaseTask {
            List<T> res = new List<T>();

            List<T> list = MyCache.getList<T>(typeof(T));

            foreach (BaseTask bt in list) {
                if (bt.status == (int)CoreTaskParamStatus.执行结束 || getCNo() != bt.driveId) {
                    continue;
                }
                if (!bt.canWork()) {
                    continue;
                }
                if (!needWork(bt)) {
                    continue;
                }
                res.Add((T)bt);

            }
            if (list.Count > 0) {
                consoleOnly(InfoCode.consleOnly_task_count,"共{0}个任务，可执行{1}个{2}",list.Count,res.Count,typeof(T).Name);
            }
            return res;
        }

        public virtual bool needWork(BaseTask bt) {
            return true;
        }
        public bool working(int status) {
            if (status > (int)CoreTaskParamStatus.待执行 && status < (int)CoreTaskParamStatus.执行结束) {
                return true;
            } else {
                return false;
            }
        }
    }
}
